package com.iking.housekeeping.controller.changeservice;

import com.iking.housekeeping.common.CodeEnum;
import com.iking.housekeeping.common.ReturnCode;
import com.iking.housekeeping.entity.changeservice.ChangeService;
import com.iking.housekeeping.entity.dept.SysDept;
import com.iking.housekeeping.entity.servicerecord.ServiceRecord;
import com.iking.housekeeping.service.changeService.ChangeServiceS;
import com.iking.housekeeping.service.dept.DeptService;
import com.iking.housekeeping.util.token.WebTokenUtil;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * 更改服务记录
 *
 * @author: cj
 * @date: 2018年5月14日14:55:27
 */
@RestController
@RequestMapping("/changeService")
public class ChangeServiceController {
    @Autowired
   private ChangeServiceS changeServiceS;

    @Autowired
    private DeptService deptService;
    /**
     *
     * @Description: 查询所有要更改的服务
     * @author:cj
     * @return
     */
    @GetMapping("/selectAllChange")
    public ReturnCode selectAllChange ( javax.servlet.http.HttpServletRequest request){
        ChangeService changeService=new ChangeService();
        // 获取当前登录人id
        String userId = WebTokenUtil.getUserIdByRequest(request);
        List<SysDept> menuList = deptService.selectAdminById(userId);
        List list = deptService.selectAdminById(userId);
        // 判断当前登录人是否为区域管理员
        if ("".equals(menuList.get(0).getfDeptPCode()) || menuList.get(0).getfDeptPCode() == null) {
            // 查看所有区域数据
            changeService.setfStreetCommunity(menuList.get(0).getfId());
        } else {
            // 查看本服务区变更数据
            changeService.setfStreetCommunity(menuList.get(0).getfId());

        }
        List<ChangeService> changeList =changeServiceS.selectAllChange(changeService);
        if(changeList.size()<0){
            return new ReturnCode(CodeEnum.FAIL,null);
        }
        return new ReturnCode(CodeEnum.SUCCESS, "操作成功！",changeList);
    }

    /**
     *
     * @Description: 根据id查询要更改的服务
     * @author:cj
     * @param id
     * @return
     */
    @GetMapping("/selectByElderlyId/{id}")
    public ReturnCode selectByElderlyId(@PathVariable("id") String id){
        if("".equals(id) && id == null){
            return new ReturnCode(CodeEnum.FAIL_EARROR_PARM, "请选择要更改的服务！", null);
        }
        ChangeService changeService = changeServiceS.selectById(id);
        if(changeService == null){
            return new ReturnCode(CodeEnum.FAIL,null);
        }
        return new ReturnCode(CodeEnum.SUCCESS, "操作成功！",changeService);
    };

    /**
     *
     * @Description:更改服务
     * @author:CGZ
     * @param record
     * @return
     */
    @PostMapping("/changeService")
    public ReturnCode changeService(@RequestBody @Valid ChangeService record, BindingResult bindingResult) {
        // 参数不为空验证
        if (bindingResult.hasErrors()) {
            String msg = bindingResult.getFieldError().getDefaultMessage();
            return new ReturnCode(CodeEnum.FAIL_EARROR_PARM, msg, null);
        }
          record.setfState("e08f93a0cefc4162b79a63f3db9a1737");
          record.setfGmtModified(new Date());
        int result = changeServiceS.updateService(record);
        if(result < 1) {
            return new ReturnCode(CodeEnum.FAIL,null);
        }
        return new ReturnCode(CodeEnum.SUCCESS, null);
    }
}
